package com.joke.mydiary;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;

import com.joke.mydiary.DaoImpl.DiaryDao;
import com.joke.mydiary.fragment.ShowDiaryFragment;
import com.joke.mydiary.fragment.UpdateDiaryFragment;
import com.joke.mydiary.pojo.Diary;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;

public class ShowDiaryActivity extends AppCompatActivity implements UpdateDiaryFragment.UpdateDiaryFragmentListener, ShowDiaryFragment.ShowDiaryFragmentListener {

    private DiaryDao dao;
    private FragmentManager rm;
    private ShareActionProvider asp;
    private File imageFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_diary);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //初始化参数
        dao = new DiaryDao(this);
        rm = getFragmentManager();
        //取得传递过来的对象
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");


        FragmentTransaction ft = rm.beginTransaction();
        ShowDiaryFragment sdf = ShowDiaryFragment.getInstance(bundle);
        ft.replace(R.id.show_main, sdf);
        ft.addToBackStack(null);
        ft.commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_show, menu);
        MenuItem shareItem = menu.findItem(R.id.action_share);
        asp = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
        asp.setShareIntent(menuShare());
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean deleteDiary(int id) {
        boolean flag = dao.delete(id);
        return flag;
    }


    @Override
    public void showUpdateFragment() {
        FragmentTransaction ft = rm.beginTransaction();
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        UpdateDiaryFragment udf = UpdateDiaryFragment.getInstance(bundle);
        ft.replace(R.id.show_main, udf);
        ft.addToBackStack(null);
        ft.commit();
    }

    //点击分享按钮进行分享
    @Override
    public void shareDiary(Diary diary) {
        Toast.makeText(this, "该功能暂时只能实现分享文字！", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, diary.getContent());
        startActivity(intent);
    }

    @Override
    public void startBackupService(Intent intent) {
        startService(intent);
    }


    //通过菜单分享
    public Intent menuShare() {
        Intent seedIntent = new Intent(Intent.ACTION_SEND);
        seedIntent.setType("text/plain");
        Diary diary = (Diary) getIntent().getBundleExtra("bundle").getSerializable("diary");
        seedIntent.putExtra(Intent.EXTRA_TEXT, diary.getContent());
        return seedIntent;
    }

    @Override
    public boolean update(Diary diary) {
        return dao.update(diary);
    }

    @Override
    public void showDiaryFragment(Diary diary) {
        Intent intent = getIntent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("diary", diary);
        FragmentTransaction ft = rm.beginTransaction();
        ShowDiaryFragment sdf = ShowDiaryFragment.getInstance(bundle);
        ft.replace(R.id.show_main, sdf);
        ft.addToBackStack(null);
        ft.commit();
    }

    @Override
    public InputStream getInputStreamByUrl(Uri uri) {
        try {
            return getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }


}
